package trucaso;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;


public abstract class Game{
	protected Deck deck;
	protected CardZone cardZone;
	protected Actions actions;
	protected Card[] both;
	protected ScoreBoard scores;
	protected String name,name2;
	protected boolean envido,envido2,realEnvido,faltaEnvido;
	protected boolean flor,truco,retruco,vale4,turn;
	
	protected Socket socket = null;
	protected ChatPane chatPane;
	protected DataOutputStream streamOut;
	protected DataInputStream streamIn;
	final double version = 0.24;
	
	
	protected Game(Deck d, CardZone c,Actions a,ScoreBoard sb,ChatPane cp) {
		deck = d;
		cardZone = c;
		actions = a;
		scores = sb;
		chatPane = cp;
		
		both = new Card[6];
		
		deck.shuffle();
		cardZone.game = this;
		
	}
	
	protected Card[] getBothHands() {
		return both;
	}
	
	protected void resetFlags() {
		envido = false;
		envido2 = false;
		realEnvido = false;
		faltaEnvido = false;
		flor = false;
		truco = false;
		retruco = false;
		vale4 = false;
	}
	
	protected void returnCards() {
		for(int i=0;i<both.length;i++) {
			both[i].setInGame(false);
			both[i].setVisible(false);
		}
	}
	
	protected boolean isFinished() {
		for(int i=0;i<6;i++) {
			if(both[i].isInGame()) return false;
		}
		return true;
	}
	
	public void open() throws IOException{  
		streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
		streamOut = new DataOutputStream(socket.getOutputStream());
	}
	
	public void close() throws IOException{  
		if (socket != null)    socket.close();
	    if (streamIn != null)  streamIn.close();
	}
	
	protected void playCard(int n) {
		try{
			streamOut.writeUTF("p "+n);
			streamOut.flush();
			turn= !turn;
		}catch(IOException ioex) {
			chatPane.putMessage("Game Error! You're playing against no one! Panic!");
		}
	}
	
	protected void sendVersion() {
		try {
			streamOut.writeUTF("v"+Double.toString(version));
		}catch(IOException ioex) {
			chatPane.putMessage("An Error has Ocurred!");
		}
	}
	
	protected void checkVersion(String ver) {
		double v = Double.parseDouble(ver.substring(1));
		if(version==v) chatPane.putMessage("Version check: OK!");
		if(version<v) chatPane.putMessage("WARNING: "+name2+" is running a newer Version\n" +
				"You Should upgrade yours to avoid compatibility problems.");
		if(version>v) chatPane.putMessage("WARNING: "+name2+" is running an old Version of the game.\n" +
				"You could experience problems.");
	}
	
	protected void maso() {
		try{
			streamOut.writeUTF("maso");
			streamOut.flush();
			for(int i=3;i<6;i++) {
				if(both[i].isInGame()){
					cardZone.playCard(i,false);
				}
			}
		}catch(IOException ex) {
			chatPane.putMessage("Error! There's no one on the other side!");
		}
	}
	
	protected void receiveMaso() {
		for(int i=0;i<3;i++) {
			if(both[i].isInGame()){
				cardZone.playCard(i,false);
			}
		}
	}
	
	protected void truco() {
		try{
			streamOut.writeUTF("+");
			streamOut.flush();
			truco = true;
		}catch(IOException ioe) {}
	}
	
	protected void receiveTruco() {
		actions.setRetruco();
		actions.enableQuieros();
		truco = true;
	}
	
	protected void retruco() {
		try{
			streamOut.writeUTF("*");
			streamOut.flush();
			retruco = true;
		}catch(IOException ioe) {}
	}
	
	protected void receiveRetruco() {
		actions.setValeCuatro();
		actions.enableQuieros();
		retruco =true;
		truco = true;
	}
	
	protected void vale4() {
		try{
			streamOut.writeUTF("4");
			streamOut.flush();
			vale4 = true;
		}catch(IOException ioe) {}
	}
	
	protected void receiveVale4(){
		actions.truco.setEnabled(false);
		vale4 = true;
	}
	
	
	
	protected void changeName(String nam) {
		try{
			name = nam;
			streamOut.writeUTF("n "+nam);
			streamOut.flush();
			chatPane.putMessage("You are now known as "+name);
			scores.setNames(name,name2);
			scores.repaint();
		}catch(IOException ioe) {
			chatPane.putMessage("Error while sending Identification!");
		}
	}
	
	protected void receiveName(String n) {
		String lastName = name2;
		name2 = n.substring(2);
		chatPane.putMessage("<:"+lastName+" is now known as "+name2);
		scores.setNames(name,name2);
		scores.repaint();
	}
}
